﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.ComponentModel;
using Sys.WebRes.Data;

namespace Sys.WebRes.Publish.Areas.Admin.Models
{
    /// <summary>
    /// 
    /// </summary>
    public class ResourceModel
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Thumbnail { get; set; }
        public string Keywords { get; set; }
        public string Summary { get; set; }
        public string Contents { get; set; }
        public virtual string XmlExt { get; set; }
        public string GUID { get; set; }
        public int ResTypeID { get; set; }
        public int StatusTypeID { get; set; }
        public string FilePath { get; set; }
        public string Author { get; set; }
        public int? CreatorID { get; set; }
        public int? EditorID { get; set; }
        public DateTime? MadeDate { get; set; }
        public DateTime? CreateDate { get; set; }
        public DateTime? UpdateDate { get; set; }

        public string CreatorName { get; set; }
        public string EditorName { get; set; }
        public bool AutoSave { get; set; }

        public string Tags{ get; set; }

        public List<ResAttachLinkModel> Attachments { get; set; }
        public List<SecResLinkModel> Sections { get; set; }
        public ResCountModel ResCount { get; set; }
        public GlobalResModel GlobalRes { get; set; }

        public bool IsGlobal { get; set; }
        public bool HasCount { get; set; }

        public ResourceModel()
        {
            this.Attachments = new List<ResAttachLinkModel>();
            this.Sections = new List<SecResLinkModel>();

            this.HasCount = false;
            this.ResCount = new ResCountModel();
            this.IsGlobal = false;
            this.GlobalRes = new GlobalResModel();
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public class GlobalResModel
    {
        public int ResourceID { get; set; }
        public string Name { get; set; }
        public string Abbr { get; set; }
        public int OrderNo { get; set; }
    }
    /// <summary>
    /// 
    /// </summary>
    public class ResCountModel
    {
        public int HitCount { get; set; }
        public int GoodCount { get; set; }
        public int FavorCount { get; set; }
        public int NeutralCount { get; set; }
        public int ExceptCount { get; set; }
        public int BadCount { get; set; }
        public DateTime? LastUsedDate { get; set; }
    }
    /// <summary>
    /// 
    /// </summary>
    public class SecResLinkModel
    {
        public int SectionID { get; set; }
        public int OrderNo { get; set; }
        public bool TopLevel { get; set; }
    }
    /// <summary>
    /// 
    /// </summary>
    public class ResAttachLinkModel
    {
        public int AttachmentID { get; set; }
        public string Title { get; set; }
        public string Explain { get; set; }
        public string Url { get; set; }
        public int ResAttachLinkTypeID { get; set; }
        public int OrderNo { get; set; }

        //保存数据时使用
        public Attachment Attachment { get; set; }
    }
    /// <summary>
    /// 
    /// </summary>
    public class ProductModel : ResourceModel
    {
        //产品图集
        public PicturesModel Pictures { get; set; }
        //离线说明文档
        public FilesModel OfflineFile { get; set; }
        //扩展产品信息
        public ProductExt Product { get; set; }

        /// <summary>
        /// 扩展字段
        /// </summary>
        public override string XmlExt
        {
            get
            {
                var seri = new XmlSerializer(typeof(List<ProductExt>));
                using (var writer = new StringWriter())
                {
                    seri.Serialize(writer, this.Product);
                    return writer.GetStringBuilder().ToString();
                }
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    try
                    {
                        var seri = new XmlSerializer(typeof(ProductExt));
                        this.Product = seri.Deserialize(new StringReader(value)) as ProductExt;
                    }
                    catch
                    {
                        this.Product = new ProductExt();
                    }
                }
            }
        }

        public ProductModel(): base()
        {
            this.Product = new ProductExt();
        }
    }
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class ProductExt
    {
        public string ProductID { get; set; }
        public string Type { get; set; }
        public string Price { get; set; }
        public string WPrice { get; set; }
        public string Quantity { get; set; }
        public string Color { get; set; }
        public string Size { get; set; }
        public string Weight { get; set; }
        public string SellUrl { get; set; }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class FilesModel<T> : ResourceModel where T : FileModel, new()
    {
        public List<T> Items { get; set; }

        /// <summary>
        /// 扩展字段
        /// </summary>
        //public override string XmlExt
        //{
        //    get
        //    {
        //        var seri = new XmlSerializer(typeof(List<T>));
        //        using (var writer = new StringWriter())
        //        {
        //            seri.Serialize(writer, this.Items);
        //            return writer.GetStringBuilder().ToString();
        //        }
        //    }
        //    set
        //    {
        //        if (string.IsNullOrEmpty(value))
        //        {
        //            this.Items.Add(new T());
        //        }
        //        else
        //        {
        //            var seri = new XmlSerializer(typeof(List<T>));
        //            this.Items = seri.Deserialize(new StringReader(value)) as List<T>;
        //        }
        //    }
        //}

        public FilesModel():base()
        {
            this.Items = new List<T>();
        }
    }
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class FileModel : AttachmentModel
    {
        private string Url_;
        public string Url 
        {
            get
            {
                if (!string.IsNullOrEmpty(Url_))
                {
                    return this.Url_;
                }
                else if (this.ID <= 0)
                {
                    return string.Empty;
                }

                if (AttachUrlTypeID == AttachUrlType.DB.ID)
                {
                    return SysConfig.HostUrl + "Attachment/Pic?id=" + this.ID;
                }
                else // if (AttachUrlTypeID == AttachUrlType.MaskPath.ID)
                {
                    return SysConfig.HostUrl + "Attachment/Pic?path=" + this.FilePath;
                }
            }
            set
            {
                Url_ = value;
            }
        }
        public int? AttachUrlTypeID { get; set; }
        public string ThumbUrl { get { return this.Url + "&t=true"; } }
    }
    /// <summary>
    /// 
    /// </summary>
    public class FilesModel : FilesModel<FileModel>
    {
    }
    /// <summary>
    /// 
    /// </summary>
    public class PicturesModel : FilesModel
    {
    }

    /// <summary>
    /// 
    /// </summary>
    public class MediasModel : FilesModel
    {
    }
}